#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QAction>
#include <QColorDialog>
#include <myhutu.h>
#include <QTabBar>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QToolBar *tbar = addToolBar(tr("工具栏"));
    tbar->setMovable(false);
    tbar->setIconSize(QSize(16, 16));
    tbar->setStyleSheet("background-color:rgb(199,237,204)");

    pRed = new QAction(tr("&红色"), this);
    pRed->setCheckable(true);
    pRed->setChecked(true);
    tbar->addAction(pRed);

    pGreen = new QAction(tr("&绿色"), this);
    pGreen->setCheckable(true);
    tbar->addAction(pGreen);

    pBlue = new QAction(tr("&蓝色"), this);
    pBlue->setCheckable(true);
    tbar->addAction(pBlue);

    pCustomColor = new QAction(tr("&自定义颜色"));
    tbar->addAction(pCustomColor);

    openAction = new QAction(tr("&打开"), this);
    openAction->setIcon(QIcon("e:/open.png"));
    openAction->setShortcut(QKeySequence(tr("Ctrl+O")));
    tbar->addAction(openAction);

    saveAction = new QAction(tr("&保存"), this);
    saveAction->setIcon(QIcon("e:/save.png"));
    saveAction->setShortcut(QKeySequence(tr("Ctrl+S")));
    tbar->addAction(saveAction);

    saveasAction = new QAction(tr("&另存为"), this);
    saveasAction->setIcon(QIcon("e:/saveas.png"));
    saveasAction->setShortcut(QKeySequence(tr("Ctrl+ALT+S")));
    tbar->addAction(saveasAction);

    lineAction = new QAction(tr("&直线"), this);
    lineAction->setIcon(QIcon("e:/zx.png"));
    lineAction->setShortcut(QKeySequence(tr("Ctrl+L")));
    tbar->addAction(lineAction);

    linesAction = new QAction(tr("&铅笔"), this);
    linesAction->setIcon(QIcon("e:/qb.png"));
    linesAction->setShortcut(QKeySequence(tr("Ctrl+P")));
    tbar->addAction(linesAction);

    rectAction = new QAction(tr("&矩形"), this);
    rectAction->setIcon(QIcon("e:/rect.png"));
    rectAction->setShortcut(QKeySequence(tr("Ctrl+R")));
    tbar->addAction(rectAction);

    ellipseAction = new QAction(tr("&椭圆"), this);
    ellipseAction->setIcon(QIcon("e:/ellipse.png"));
    ellipseAction->setShortcut(QKeySequence(tr("Ctrl+E")));
    tbar->addAction(ellipseAction);

    textAction = new QAction(tr("&文字"), this);
    textAction->setIcon(QIcon("e:/texts.png"));
    textAction->setShortcut(QKeySequence(tr("Ctrl+T")));
    tbar->addAction(textAction);

    undoAction = new QAction(tr("&撤销"), this);
    undoAction->setIcon(QIcon("e:/undo.png"));
    undoAction->setShortcut(QKeySequence(tr("Ctrl+Z")));
    tbar->addAction(undoAction);

    connect(pRed, SIGNAL(triggered(bool)), this, SLOT(rSlot(bool)));
    connect(pGreen, SIGNAL(triggered(bool)), this, SLOT(gSlot(bool)));
    connect(pBlue, SIGNAL(triggered(bool)), this, SLOT(bSlot(bool)));
    connect(pCustomColor, SIGNAL(triggered(bool)), this, SLOT(costomColorSlot(bool)));
    connect(lineAction, SIGNAL(triggered(bool)), this, SLOT(on_lineAction_triggered(bool)));
    connect(saveAction, SIGNAL(triggered(bool)), this, SLOT(on_saveAction_triggered(bool)));
    connect(openAction, SIGNAL(triggered(bool)), this, SLOT(on_openAction_triggered(bool)));
    connect(textAction, SIGNAL(triggered(bool)), this, SLOT(on_textAction_triggered(bool)));
    connect(linesAction, SIGNAL(triggered(bool)), this, SLOT(on_linesAction_triggered(bool)));
    connect(ellipseAction, SIGNAL(triggered(bool)), this, SLOT(on_ellipseAction_triggered(bool)));
    connect(rectAction , SIGNAL(triggered(bool)), this, SLOT(on_rectAction_triggered(bool)));
    connect(saveasAction , SIGNAL(triggered(bool)), this, SLOT(on_saveasAction_triggered(bool)));
    connect(undoAction , SIGNAL(triggered(bool)), this, SLOT(on_undoAction_triggered(bool)));


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::rSlot(bool checked)
{
    ui->frame->color = QColor(255, 0, 0);
    pRed->setChecked(true);
    pGreen->setChecked(false);
    pBlue->setChecked(false);
}

void MainWindow::gSlot(bool checked)
{
    ui->frame->color = QColor(0, 255, 0);
    pRed->setChecked(false);
    pGreen->setChecked(true);
    pBlue->setChecked(false);
}

void MainWindow::bSlot(bool checked)
{
    ui->frame->color = QColor(0, 0, 255);
    pRed->setChecked(false);
    pGreen->setChecked(false);
    pBlue->setChecked(true);
}

void MainWindow::costomColorSlot(bool checked)
{
    QColorDialog dlg;
    if (dlg.exec() == QDialog::Accepted) {
        ui->frame->color = dlg.currentColor();
        pRed->setChecked(false);
        pGreen->setChecked(false);
        pBlue->setChecked(false);
    }
}

void MainWindow::on_radioButton_2_clicked() // 2
{
    ui->frame->color = QColor(0, 255, 0);
    pRed->setChecked(false);
    pGreen->setChecked(true);
    pBlue->setChecked(false);
}

void MainWindow::on_radioButton_clicked() // 1
{
    ui->frame->color = QColor(255, 0, 0);
    pRed->setChecked(true);
    pGreen->setChecked(false);
    pBlue->setChecked(false);
}

void MainWindow::on_radioButton_3_clicked() // 3
{
    ui->frame->color = QColor(0, 0, 255);
    pRed->setChecked(false);
    pGreen->setChecked(false);
    pBlue->setChecked(true);
}

void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    ui->label->setText(QString("%1").arg(value + 1)); // 当这个滑块移动时，文本内容改变
    ui->frame->linesize = value;
}

void MainWindow::on_lineAction_triggered(bool checked)
{
    // 更新起始点和终点
    ui->frame->isDrawingLine = true;
    ui->frame->isDrawingCurve = false;
    ui->frame->isDrawingEllips=false;
    ui->frame->isDrawingRect=false;
    lineAction->setChecked(true);
}

void MainWindow::on_saveAction_triggered(bool checked)
{
    ui->frame->SavePic(); // 保存
}
void MainWindow::on_saveasAction_triggered(bool checked)
{
    ui->frame->SaveAsPic(); // 保存
}

void MainWindow::on_openAction_triggered(bool checked)
{
    ui->frame->OpenPic(); // 打开
}


void MainWindow::on_linesAction_triggered(bool checked)
{
    ui->frame->isDrawingCurve=true;
    ui->frame->isDrawingLine = false;
    ui->frame->isDrawingEllips=false;
    ui->frame->isDrawingRect=false;
    linesAction->setChecked(true);
}
void MainWindow::on_ellipseAction_triggered(bool checked)
{
    ui->frame->isDrawingEllips=true;
    ui->frame->isDrawingLine = false;
    ui->frame->isDrawingCurve=false;
    ui->frame->isDrawingRect=false;
    ellipseAction->setChecked(true);
}
void MainWindow::on_rectAction_triggered(bool checked)
{
    ui->frame->isDrawingCurve=false;
    ui->frame->isDrawingLine=false;
    ui->frame->isDrawingEllips=false;
    ui->frame->isDrawingRect=true;
    rectAction->setChecked(true);
}
void MainWindow::on_textAction_triggered(bool checked)
{
    ui->frame->isAddText=true;
    ui->frame->AddTexts();
     textAction->setChecked(true);
}
void MainWindow::on_undoAction_triggered(bool checked)
{
    ui->frame->undo();
   undoAction->setChecked(true);
}
